home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
xlib
/
readwrite.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
9KB
|
363 lines
/*
* (c) Copyright 1994, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
#include <GL/glx.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <X11/keysym.h>
static int RGB_SB_attributes[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
static int CI_SB_attributes[] = {
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
static int RGB_DB_attributes[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DOUBLEBUFFER,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
static int CI_DB_attributes[] = {
GLX_DOUBLEBUFFER,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
Display *dpy;
Window window;
int rgb = 1;
GLenum direct = GL_TRUE;
int W = 600;
int H = 600;
int doubleBuf = 1;
int dither = 1;
char rawData[1280*1024*4]; /* big enough for a cheap demo */
static void Init(void)
{
if (rgb) {
glClearColor(0,0,0,0);
} else {
glClearIndex(0);
}
glClear(GL_COLOR_BUFFER_BIT);
glReadBuffer(GL_FRONT);
glDrawBuffer(GL_FRONT);
}
static void Redraw(void)
{
int i;
int r,g;
glViewport(0,0,W,H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,W,0,H,-1,1);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
r = g = 0;
glIndexf(0);
glBegin(GL_QUAD_STRIP);
glColor3ub(r,g,0);
glVertex2f(0, 0);
glColor3ub(r,g,255);
glVertex2f(0, H);
r = 255;
glIndexf(85);
glColor3ub(r,g,0);
glVertex2f(W/3, 0);
glColor3ub(r,g,255);
glVertex2f(W/3, H);
g = 255;
glIndexf(170);
glColor3ub(r,g,0);
glVertex2f(W*2/3, 0);
glColor3ub(r,g,255);
glVertex2f(W*2/3, H);
r = 0;
glIndexf(255);
glColor3ub(r,g,0);
glVertex2f(W, 0);
glColor3ub(r,g,255);
glVertex2f(W, H);
glEnd();
}
static void pixelsTest(void)
{
if (rgb) {
glReadPixels(0,0,W,H,GL_RGBA,GL_UNSIGNED_BYTE,rawData);
glRasterPos2f(0,0);
glDrawPixels(W,H,GL_RGBA,GL_UNSIGNED_BYTE,rawData);
} else {
glReadPixels(0,0,W,H,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,rawData);
glRasterPos2f(0,0);
glDrawPixels(W,H,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,rawData);
}
}
static void blendTest(void)
{
if (!rgb) {
printf("blend Test not supported in color index mode.\n");
return;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0, 1.0, 1.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(0,0);
glVertex2f(W,0);
glVertex2f(W,H);
glVertex2f(0,H);
glEnd();
glDisable(GL_BLEND);
glFlush();
}
static void Usage(void)
{
fprintf(stderr, "Usage: program [-c] [-s] [-i]\n");
fprintf(stderr, " -c run in color index mode\n");
fprintf(stderr, " -s run in singlebuffer mode\n");
fprintf(stderr, " -i turn direct rendering off--go thru the X server\n");
exit(1);
}
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
int main(int argc, char** argv)
{
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needDisplay;
XColor white;
int i;
rgb = 1;
direct = GL_TRUE;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'c':
rgb = GL_FALSE;
break;
case 's':
doubleBuf = 0;
break;
case 'i':
direct = GL_FALSE;
break;
default:
Usage();
}
} else {
Usage();
}
}
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
return -1;
}
vi = glXChooseVisual(dpy, DefaultScreen(dpy),
doubleBuf ? (rgb ? RGB_DB_attributes : CI_DB_attributes) :
(rgb ? RGB_SB_attributes : CI_SB_attributes));
if (!vi) {
fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
getenv("DISPLAY"));
return -1;
}
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
rgb ? AllocNone : AllocAll);
if (rgb) {
white.red = ~0;
white.green = ~0;
white.blue = ~0;
XAllocColor(dpy, cmap, &white);
swa.background_pixel = white.pixel;
} else {
swa.background_pixel = 15;
}
if (!rgb) {
XColor buf;
int i;
buf.flags = DoRed | DoGreen | DoBlue;
/* Init color map */
for (i=0; i<16; i++) {
buf.pixel = i;
buf.blue = (i & 4) ? 65535 : 0;
buf.green = (i & 2) ? 65535 : 0;
buf.red = (i & 1) ? 65535 : 0;
if (i > 8) {
buf.red /= 2;
buf.green /= 2;
buf.blue /= 2;
}
XStoreColor(dpy, cmap, &buf);
}
}
swa.border_pixel = 0;
swa.background_pixel = white.pixel;
swa.colormap = cmap;
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
| KeyReleaseMask;
window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
W, H,
0, vi->depth, InputOutput, vi->visual,
CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
&swa);
XSetWMColormapWindows(dpy, window, &window, 1);
XMapWindow(dpy, window);
XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
cx = glXCreateContext(dpy, vi, 0, direct);
if (!glXMakeCurrent(dpy, window, cx)) {
fprintf(stderr, "Can't make window current to context\n");
return -1;
}
Init();
needDisplay = GL_TRUE;
for (;;) {
do {
XNextEvent(dpy, &event);
switch (event.type) {
case Expose:
needDisplay = GL_TRUE;
break;
case ConfigureNotify:
W = event.xconfigure.width;
H = event.xconfigure.height;
needDisplay = GL_TRUE;
break;
case KeyPress:
{
char buf[100];
int rv;
KeySym ks;
rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
switch (ks) {
case XK_P: case XK_p:
printf("pixels test (ReadPixels followed by DrawPixels)\n");
pixelsTest();
break;
case XK_B: case XK_b:
printf("blend test\n");
blendTest();
break;
case XK_R: case XK_r:
needDisplay = GL_TRUE;
break;
case XK_D: case XK_d:
dither = 1-dither;
if (dither) {
glEnable(GL_DITHER);
printf("Dithering on\n");
} else {
glDisable(GL_DITHER);
printf("Dithering off\n");
}
needDisplay = GL_TRUE;
break;
case XK_Escape:
return 0;
default:
break;
}
}
break;
}
} while (XPending(dpy) != 0);
if (needDisplay) {
needDisplay = GL_FALSE;
Redraw();
}
}
}